// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.chimi.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.constant.ThreadLocalConstants;
import com.liuxinlong.modules.chimi.controller.request.QueryBarBrowserReq;
import com.liuxinlong.modules.chimi.controller.request.QueryBarPointFlowReq;
import com.liuxinlong.modules.chimi.controller.request.UpdateBarCardReq;
import com.liuxinlong.modules.chimi.controller.request.UpdateBarCommentReq;
import com.liuxinlong.modules.chimi.service.BarBrowseService;
import com.liuxinlong.modules.chimi.service.BarLuckService;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.BarCard;
import com.liuxinlong.modules.entity.BarComment;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.utils.ThreadlocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 论坛浏览控制层
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-07-07
 */
@RestController
@RequestMapping("/bar/browse")
public class BarBrowseController {
    @Autowired
    private BarBrowseService barBrowseService;

    @Autowired
    private BarLuckService barLuckService;
    /**
     * 论坛首页
     *
     * @return 论坛首页信息
     */
    @GetMapping("home")
//    @LogAnnotation(value = "首页查询", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp queryHomeBar() {
        Map<String, Object> resultMap = barBrowseService.queryHomeBar();
        BaseResp resp = new BaseResp();
        resp.setDataMap(resultMap);
        resp.setRetMessage("查询论坛首页成功");
        return resp;
    }

    /**
     * 分页查询发帖信息
     *
     * @param req 请求信息
     * @return 用户发帖信息列表
     */
    @PostMapping("page")
//    @LogAnnotation(value = "分页查询", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageCardInfo(@RequestBody QueryBarBrowserReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("plate", req.getId());
        queryMap.put("keyword", req.getKeyword());
        User current = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        queryMap.put("currentUser", current.getId());
        List<Map<String, Object>> dataList = barBrowseService.pageCard(queryMap);
        int totalNum = barBrowseService.getCardCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询帖子信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 查询帖子
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("query")
    public BaseResp getBarCard(@RequestBody QueryBaseReq req) {
        Map<String,Object> dataMap = barBrowseService.getBarCard(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataMap(dataMap);
        resp.setRetMessage("查询帖子信息成功");
        return resp;
    }

    /**
     * 新增帖子
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("add")
    @LogAnnotation(value = "新增帖子信息", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addBarCard(@RequestBody UpdateBarCardReq req) {
        BarCard barCard = new BarCard();
        barCard.setPlate(req.getPlate());
        barCard.setReleaseType(req.getReleaseType());
        barCard.setTitle(req.getTitle());
        barCard.setContent(req.getContent());
        barCard.setAttach(req.getAttach());
        barBrowseService.addBarCard(barCard);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增帖子信息成功");
        return resp;
    }

    /**
     * 修改帖子信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("update")
    @LogAnnotation(value = "修改帖子信息", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateBarCard(@RequestBody UpdateBarCardReq req) {
        BarCard barCard = new BarCard();
        barCard.setId(req.getId());
        barCard.setReleaseType(req.getReleaseType());
        barCard.setTitle(req.getTitle());
        barCard.setPlate(req.getPlate());
        barCard.setContent(req.getContent());
        barCard.setAttach(req.getAttach());
        barBrowseService.updateBarCard(barCard);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改帖子信息成功");
        return resp;
    }

    /**
     * 删除帖子信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("delete")
    @LogAnnotation(value = "删除帖子信息", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteBarCard(@RequestBody BaseDeleteReq req) {
        barBrowseService.deleteBarCard(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除帖子信息成功");
        return resp;
    }

    /**
     * 分页查询发帖评论信息
     *
     * @param req 请求信息
     * @return 用户发帖评论信息列表
     */
    @PostMapping("comment/page")
//    @LogAnnotation(value = "分页查询帖子评论信息", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp pageCommentInfo(@RequestBody QueryBaseReq req) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startNum", req.getStartNum());
        queryMap.put("pageSize", req.getPageSize());
        queryMap.put("cardId", req.getId());
        User current = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        queryMap.put("currentUser", current.getId());
        List<Map<String, Object>> dataList = barBrowseService.pageCardComment(queryMap);
        int totalNum = barBrowseService.getCardCommentCount(queryMap);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("分页查询评论信息成功");
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        return resp;
    }

    /**
     * 新增帖子评论
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("comment/add")
    @LogAnnotation(value = "新增帖子评论信息", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_ADD)
    public BaseResp addComment(@RequestBody UpdateBarCommentReq req) {
        BarComment barComment = new BarComment();
        barComment.setCardId(req.getParent());
        barComment.setReply(req.getReply());
        barComment.setReplyUser(req.getReplyUser());
        barComment.setContent(req.getContent());
        barComment.setAttach(req.getAttach());
        barBrowseService.addComment(barComment);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("新增帖子评论信息成功");
        return resp;
    }

    /**
     * 修改帖子评论信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("comment/update")
    @LogAnnotation(value = "修改帖子评论信息", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp updateComment(@RequestBody UpdateBarCommentReq req) {
        BarComment barComment = new BarComment();
        barComment.setId(req.getId());
        barComment.setCardId(req.getParent());
//        barComment.setReply(req.getReply());
//        barComment.setReplyUser(req.getReplyUser());
        barComment.setContent(req.getContent());
//        barComment.setAttach(req.getAttach());
        barBrowseService.updateComment(barComment);
        BaseResp resp = new BaseResp();
        resp.setRetMessage("修改帖子评论信息成功");
        return resp;
    }

    /**
     * 删除帖子评论信息
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("comment/delete")
    @LogAnnotation(value = "删除帖子评论信息", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_DELETE)
    public BaseResp deleteComment(@RequestBody BaseDeleteReq req) {
        barBrowseService.deleteComment(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("删除帖子评论信息成功");
        return resp;
    }

    /**
     * 点赞帖子
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("likes")
    @LogAnnotation(value = "帖子点赞", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp likeCard(@RequestBody BaseDeleteReq req) {
        barBrowseService.likeCard(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("帖子点赞成功");
        return resp;
    }

    /**
     * 点赞帖子评论
     *
     * @param req 请求信息
     * @return 成功标识
     */
    @PostMapping("comment/likes")
    @LogAnnotation(value = "评论点赞", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp likeComment(@RequestBody BaseDeleteReq req) {
        barBrowseService.likeComment(req.getId());
        BaseResp resp = new BaseResp();
        resp.setRetMessage("评论点赞成功");
        return resp;
    }

    /**
     * 论坛用户签到
     *
     * @return 成功标识
     */
    @GetMapping("sign")
    @LogAnnotation(value = "用户签到", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp userSign() {
        Map<String, Object> resultMap = barBrowseService.userSign();
        BaseResp resp = new BaseResp();
        resp.setDataMap(resultMap);
        resp.setRetMessage("用户签到成功");
        return resp;
    }

    /**
     * 论坛用户积分记录
     *
     * @return 成功标识
     */
    @PostMapping("point/record")
    @LogAnnotation(value = "用户积分记录", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp queryPointRecord(@RequestBody QueryBarPointFlowReq req) {
        Map<String, Object> queryParam = new HashMap<>();
        queryParam.put("startNum", req.getStartNum());
        queryParam.put("pageSize", req.getPageSize());
        queryParam.put("type", req.getSourceType());
        queryParam.put("startTime", req.getStartTime());
        queryParam.put("endTime", req.getEndTime());
        User currentUser = (User)ThreadlocalUtils.getCache("userInfo");
        queryParam.put("userId", currentUser.getId());
        List<Map<String, Object>> dataList = barBrowseService.queryPointRecord(queryParam);
        int totalNum = barBrowseService.getPointRecordCount(queryParam);
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(totalNum);
        resp.setRetMessage("查询用户积分记录成功");
        return resp;
    }

    /**
     * 查询论坛抽奖奖品信息
     *
     * @return 论坛抽奖奖品信息列表
     */
    @PostMapping("luck-prize/list")
    @LogAnnotation(value = "当前抽奖奖品信息", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_QUERY)
    public BaseResp queryLuckPrizeList(@RequestBody QueryBaseReq req) {
        List<Map<String, Object>> dataList = barBrowseService.queryLuckPrizeList(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setRetMessage("查询论坛抽奖奖品信息成功");
        return resp;
    }

    /**
     * 论坛抽奖
     *
     * @return 用户论坛抽奖结果
     */
    @PostMapping("luck-prize/start")
    @LogAnnotation(value = "用户抽奖", business = LogBusinessConstants.BAR_BROWSE, operateType = LogOperateConstants.OPERATE_MODIFY)
    public BaseResp startLuckPrize(@RequestBody QueryBaseReq req) {
        Map<String, Object> resultMap = barLuckService.startLottery(req.getId());
        BaseResp resp = new BaseResp();
        resp.setDataMap(resultMap);
        resp.setRetMessage("用户抽奖成功");
        return resp;
    }

    /**
     * 轮播信息查询
     *
     * @return 轮播信息
     */
    @GetMapping("carousel")
    public BaseResp queryCarouselList() {
        List<Map<String, Object>> dataList = barBrowseService.queryCarouselList();
        BaseResp resp = new BaseResp();
        resp.setDataList(dataList);
        resp.setTotalNum(dataList.size());
        resp.setRetMessage("轮播信息查询成功");
        return resp;
    }
}
